package com.xiaoye.qbproject.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoye.qbproject.domain.MqConfig;
import com.xiaoye.qbproject.mapper.MqConfigMapper;
import com.xiaoye.qbproject.service.MqConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Administrator
 * @description 针对表【MQ_CONFIG(mq配置表)】的数据库操作Service实现
 * @createDate 2025-04-26 20:39:05
 */
@Slf4j
@Service
public class MqConfigServiceImpl extends ServiceImpl<MqConfigMapper, MqConfig>
        implements MqConfigService {
    /*@Autowired
    private RocketMQDynamicManager rocketMQDynamicManager;*/

    @Override
    @Transactional
    public void addMq(MqConfig mqConfig) {
        String nameServer = mqConfig.getNameServer();
        String producerGroup = mqConfig.getProducerGroup();
        String topic = mqConfig.getTopic();
        try {
            //rocketMQDynamicManager.createProducer(producerGroup, nameServer);
            log.info("创建生产者成功");
            /*rocketMQDynamicManager.createConsumer(
                    producerGroup,
                    nameServer,
                    topic,
                    "*",
                    new DemoMessageListener()
            );*/
            //log.info("创建消费者成功");
        } catch (Exception e) {
            log.error("添加MQ配置失败",e);
            throw new RuntimeException(e);
        }
        baseMapper.insert(mqConfig);
    }

    @Override
    public void editMqConfig(MqConfig mqConfig) {
        String nameServer = mqConfig.getNameServer();
        String producerGroup = mqConfig.getProducerGroup();
        String topic = mqConfig.getTopic();
        try {
            //rocketMQDynamicManager.destroyProducer(producerGroup);
            //rocketMQDynamicManager.destroyConsumer(producerGroup, topic);


            //rocketMQDynamicManager.createProducer(producerGroup, nameServer);
            log.info("创建生产者成功");
            /*rocketMQDynamicManager.createConsumer(
                    producerGroup,
                    nameServer,
                    topic,
                    "*",
                    new DemoMessageListener()
            );*/
            //log.info("创建消费者成功");
        } catch (Exception e) {
            log.error("修改MQ配置失败",e);
            throw new RuntimeException(e);
        }
        baseMapper.delete(Wrappers.lambdaQuery());
        baseMapper.insert(mqConfig);
    }
}




